package com.small.flowable.modules.app.controllers;

import com.common.basis.result.Result;
import com.common.basis.result.ResultSet;
import com.small.flowable.modules.app.dao.entities.FormDeployVo;
import com.small.flowable.modules.app.dao.entities.FormSubmitStartVo;
import com.small.flowable.modules.app.dao.entities.FormSubmitVo;
import lombok.AllArgsConstructor;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.FormService;
import org.flowable.engine.TaskService;
import org.flowable.engine.form.StartFormData;
import org.flowable.form.api.FormInfo;
import org.flowable.form.api.FormModel;
import org.flowable.form.api.FormRepositoryService;
import org.flowable.ui.modeler.domain.AbstractModel;
import org.flowable.ui.modeler.domain.Model;
import org.flowable.ui.modeler.serviceapi.ModelService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 表单
 *
 * @author wangguochao
 */
@RequestMapping("form")
@RestController
@AllArgsConstructor
public class FormController {
    private final ModelService modelService;
    private final FormRepositoryService formRepositoryService;
    private final FormService formService;
    private final TaskService taskService;

    /**
     * 表单模型列表
     */
    @GetMapping
    public Result<List<AbstractModel>> getList() {
        List<AbstractModel> modelsByModelType = modelService.getModelsByModelType(2);
        return ResultSet.go(modelsByModelType);
    }

    /**
     * 部署表单
     */
    @PostMapping()
    public Object deploy(@RequestBody FormDeployVo param) {
        Model model = modelService.getModel(param.getModelId());
        formRepositoryService.createDeployment()
                .name(model.getName())
                .addString(model.getName() + ".form", model.getModelEditorJson())
                .deploy();
        return ResultSet.success();
    }


    /**
     * 提交表单
     *
     * @param param 提交
     */
    @PostMapping("submit")
    public Result<?> submit(@RequestBody @Validated FormSubmitVo param,
                            org.springframework.security.core.Authentication authentication) {
        Authentication.setAuthenticatedUserId(authentication.getName());
        if (param.getMessage() != null) {
            taskService.addComment(param.getTaskId(), param.getProcessInstanceId(), param.getMessage());
        }
        if ( param.getCandidate().isEmpty()) {
            formService.submitTaskFormData(param.getTaskId(), param.getProperties());
        } else {
            taskService.complete(param.getTaskId());
            for (String s : param.getCandidate()) {
                taskService.addCandidateUser(param.getTaskId(), s);
            }
        }
        return ResultSet.success();
    }

    /**
     * 开始表单
     *
     * @param processDefinitionId 部署ID
     * @return 表单
     */
    @GetMapping("/getRenderedStartForm")
    public Result<FormModel> getRenderedStartForm(String processDefinitionId) {
        StartFormData startFormData = formService.getStartFormData(processDefinitionId);
        FormInfo formModel = formRepositoryService.getFormModelByKey(startFormData.getFormKey());
        return ResultSet.go(formModel.getFormModel());
    }

    /**
     * 表单启动
     *
     * @param param 表单
     */
    @PostMapping("submitStartFormData")
    public Result<?> formStart(@RequestBody @Validated FormSubmitStartVo param,
                               org.springframework.security.core.Authentication authentication) {
        Authentication.setAuthenticatedUserId(authentication.getName());
        formService.submitStartFormData(param.getProcessDefinitionId(), param.getBusinessKey(), param.getProperties());
        return ResultSet.success();
    }


}
